/*
 * @Author: zhumeng@sensorsdata.cn
 * @Date: 2024-04-02 19:43:42
 * @LastEditTime: 2024-04-02 19:53:49
 * @Description:
 */
import { StatsigProvider } from 'statsig-react'
import statsig from 'statsig-js'
import { useEffect, useState } from 'react'

export const BaseDemo = () => {
  const [text, setText] = useState(false)

  useEffect(() => {
    const text = statsig.getExperiment('zmtest').get('test', 'Statsig!')
    setText(text)

    statsig.logEvent('load_demo', 'SKU_12345', {
      price: '9.99',
      item_name: 'diet_coke_48_pack'
    })
    statsig.flushEvents() // o
  }, [])

  const [ready, setReady] = useState(false)

  useEffect(() => {
    statsig
      .initialize('client-vNHSZWTFYa9mn4WtyG7BNig2LX9Wq2O7dl4yf7yquzI', {
        userID: 'zm_test_sensorsdate'
      })
      .then(() => {
        setReady(true)
      })
  }, [])

  if (!ready) return null

  return (
    <StatsigProvider
      sdkKey="client-vNHSZWTFYa9mn4WtyG7BNig2LX9Wq2O7dl4yf7yquzI"
      waitForInitialization={true}
      // StatsigOptions (Not Required)
      options={{
        environment: { tier: 'staging' }
      }}
    >
      {text}
    </StatsigProvider>
  )
}
